package com.gfxy.controller;


import com.gfxy.mapper.NoticeMapper;
import com.gfxy.service.INoticeService;
import com.gfxy.service.impl.NoticeService;
import com.gfxy.vo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    INoticeService inoticeService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeMapper noticeMapper;

    @RequestMapping("/toNoticeManagePage")
    public ModelAndView toNoticeManagePage(){
        // 查寻 列表数据
        List<Notice> noticeList = noticeService.selectNoticeList(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("notice");
        modelAndView.addObject("noticeList", noticeList);

        return modelAndView;
    }

    @RequestMapping("/toAddNoticePage")
    public String toAddNoticePage(){
        return "addNoticePage";
    }

    @RequestMapping("/toUpdateNoticePage")
    public ModelAndView toUpdateNoticePage(@RequestParam("noticeId") Integer noticeId){
        ModelAndView modelAndView = new ModelAndView();
        //根据 noticeId 查询通告
        Notice notice = inoticeService.selectNoticeById(noticeId);
        modelAndView.addObject("notice", notice);
        modelAndView.setViewName("updateNoticePage");

        return modelAndView;
    }

    @RequestMapping("/addNotice")
    public String addNotice(Notice notice, HttpServletRequest request, HttpServletResponse response){
        int res = inoticeService.insertNotice(notice);
        if(res>0){
            return "redirect:/toNoticeManagePage";
        }else {
            response.setContentType("text/html;charset=utf-8");
            try {
                response.getWriter().print("<script>alert('新增通告失败!');window.location.href='toAddNoticePage'</script>");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    @PostMapping("/updateNotice")
    public String updateNotice(Notice notice, HttpServletRequest request, HttpServletResponse response){
        //调 server 层修改
        int res = noticeService.updateNotice(notice);
        if(res>0){
            return "redirect:/toNoticeManagePage";
        }else {
            response.setContentType("text/html;charset=utf-8");
//            try {
////                response.getWriter().print("<script>alert('修改通告失败!');window.location.href='toUpdateNoticePage?noticeId="+ notice.getNoticeId() +"</script>");
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
        }
        return null;
    }

    @RequestMapping("/deleteNoticeId")
    @ResponseBody
    public String deleteNoticeId(@RequestParam("noticeId") Integer noticeId){
        int res = noticeService.deleteNoticeById(noticeId);

        if(res>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @GetMapping("/selectNotice")
    @ResponseBody
    public List<Notice> selectNotice (Notice notice){
        return noticeService.selectNotice(notice);
    }

}
